#import "TouchableWindow.h"

/**
 * A TouchableWindow is a window that is touchable and delegates
 * events to any subviews it contains
 * 
 * @author      James Lin
 * @version     1.0.0
 * @since       1.0.0
 */
@implementation TouchableWindow

- (id)initWithView:(UIView*)theView 
		  delegate:(id<TouchableWindowDelegate>)theDelegate {
    if (self = [super initWithFrame:[[UIScreen mainScreen] bounds]]) {
        mainView = theView;
		delegate = theDelegate;
    }
    return self;
}

- (void)sendEvent:(UIEvent*)event {
	[super sendEvent:event];
	
	NSSet* touches = [event allTouches];
	//NSSet* touches = [event touchesForGestureRecognizer:singleFingerDoubleTap];
	
	if (touches.count != 1) {
		return;
	}
	
	UITouch* touch = touches.anyObject;
	if (touch.phase != UITouchPhaseEnded) {
		return;
	}
	if ([touch.view isDescendantOfView:mainView] == NO) {
		return;
	}
	
	CGPoint touchPoint = [touch locationInView:mainView];
	NSLog(@"Touched: %f, %f", touchPoint.x, touchPoint.y);
	//NSArray* touchPoints = [NSArray arrayWithObjects:[NSString stringWithFormat:@"%f", touchPoint.x], 
	//						[NSString stringWithFormat:@"%f", touchPoint.y], nil];
	//if (touch.tapCount > 1) {
		[delegate touchedAtPoint:touchPoint];
	//}
	//if (touch.tapCount == 1) {
	//	[self performSelector:@selector(
	//}
}

- (void)dealloc {
	mainView = nil;
	delegate = nil;
    [super dealloc];
}


@end
